package com.tzpay.tzpay.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.tzpay.tzpay.R;
import com.tzpay.tzpay.common.ConstValue;
import com.tzpay.tzpay.presenter.LogoutLogicView;
import com.tzpay.tzpay.presenter.LogoutPresenter;
import com.tzpay.tzpay.ui.BaseActivity;
import com.tzpay.tzpay.utils.ApkUtil;
import com.tzpay.tzpay.utils.AppManager;

import butterknife.Bind;
import butterknife.OnClick;


/**
 * Created by chenfeiyue on 15/7/30.
 */
public class SettingActivity extends BaseActivity implements LogoutLogicView {

    @Bind(R.id.progress)
    ProgressBar progressBar;

    private LogoutPresenter presenter;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TextView tvVersion = (TextView) findViewById(R.id.tv_version_code);
        tvVersion.setText(ApkUtil.getVersionName(this));
        presenter = new LogoutPresenter(this);
        initView();
    }

    @Override
    public void setupContentView() {
        setContentView(R.layout.activity_setting);
    }

    private void initView() {
        initAppBar();
    }

    private void initAppBar() {
        setHasAppBar(false);
//        getAppBar().setLeftNavigation(R.drawable.btn_back_red, new AppBar.OnLeftClickListener() {
//            @Override
//            public void onClick(View v) {
//                SettingActivity.this.finish();
//            }
//        });
//        getAppBar().setShowRight(false);
//        getAppBar().setTitle("设置");
        changeTitle(getString(R.string.title_setting));
    }


    @Override
    public void showProgress() {
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void onFailed(String resperr) {
        progressBar.setVisibility(View.GONE);
        closeHomeActivity();
    }

    @Override
    public void onSuccess(Object result) {
        progressBar.setVisibility(View.GONE);
        closeHomeActivity();
    }

    @Override
    public Context getContext() {
        return SettingActivity.this;
    }


    @OnClick(R.id.button)
    public void onButtonClick() {
        presenter.logout();
    }

    private void closeHomeActivity() {
        spUtil.save(ConstValue.Sp.PASSWORD, "");
        AppManager.getAppManager().finishActivity(MainActivity.class);
        startActivity(new Intent(this, LoginActivity.class));
        finish();
    }
}